<script type="text/javascript">
    $(document).ready(function() {
        document.title = "easyGIS - Aplicações: GARP";
        new AjaxUpload($('#original_species_map'), {
                action: 'UploadOriginalSpeciesMap.php',
                name:	'file_original_species_map',
                onSubmit: function(file, extension) {
                        if (! (extension && /^(txt|xml|json)$/.test(extension))){
                                $('<p class="message_error"></p>').appendTo($('#message_original_species_map_upload')).text('São permitidas apenas as extensões TXT, XML e JSON.');
                                return false;
                        }
                        if($('#message_original_species_map_upload .message_error').length != 0)
                                $('#message_original_species_map_upload .message_error').hide();
                        $('#upload_original_species_map').toggle("slow");
                        $('#status_original_species_map_upload').text('Carregando...');
                },
                onComplete : function(file, response) {
                        $('#status_original_species_map_upload').hide();
                        if(response == 'success') {
                                $('<p class="message_success"></p>').appendTo($('#message_original_species_map_upload')).text(file);
                        }
                        if(response == 'error') {
                                $('<p class="message_error"></p>').appendTo($('#message_original_species_map_upload')).text("Erro durante o upload do arquivo " . file);
                        }
                }
        });

        $('#submit_garp').click(function() {
            if(validateInputFiles()) {
                $.ajax({
                        type:   'POST',
                        url:    "GarpCore.php",
                        data:   {                species_name: $('#species_name').val(),
                                    file_original_species_map: $('#message_original_species_map_upload .message_success').text()
                        },
                        beforeSend: function(){
                                $('#content').show();
                                $('#content').html('<div id="loading">Carregando...</div>');
                        },
                        success: function(response){
                                $('#header').hide();
                                $('#wrapper').hide();
                                $('#footer').hide();
                                $('#application').html(response);
                        },
                        error: function(message){
                                $('#content').html(message);
                        }
                });
            }
            else
                return false;
        });
    });

    function validateInputFiles()
    {
        if(isNullOrEmpty($('#species_name').val()))
        {
            alert("Favor informar o nome da espécie.");
            return false;
        }
        if(isNullOrEmpty($('#message_original_species_map_upload .message_success').text()))
        {
            alert("Favor informar o mapa de ocorrências da espécie.");
            return false;
        }
        return true;
    }
</script>

<h1 class="title"><a id="garp_algorithm" href="#">Algoritmo <i>GARP</i></a></h1>
<div class="post">
	<p class="bysolidline"><small>&nbsp;</small></p>
	<div class="upload">
		<form action="Home.php" method="post" enctype="multipart/form-data">
			<p>
				<label for="species_name">Nome da Espécie:</label>
                                <input id="species_name" type="text" size="40" />
			</p>

			<p id="upload_original_species_map">
				<label for="original_species_map">Mapa de Ocorrências:</label>
				<input id="original_species_map" type="file" size="40" />
			</p>
			<span id="status_original_species_map_upload"></span>
			<div id="message_original_species_map_upload"></div>

			<p>
				<input id="submit_garp" type="submit" value="Iniciar GARP" />
			</p>
		</form>
	</div>
	<div id="example3"></div>
</div>